#ifndef UNITWINDOW_H
#define UNITWINDOW_H

#include <QtGui/QWidget>
#include <QtCore/QDebug>
#include <QQueue>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMessageBox>
#include <QtNetwork>

namespace Ui {
    class UnitWidget;
}

class UnitWidget : public QWidget
{
    Q_OBJECT

private slots:
        void newDatabase();
        void tcpOpen();

public:

    //temporary until seperate unit object developed
    class UnitData
    {
    public:
        //temporary as strings , easy conversion from rs232
        //serial message is in this order
        // ASAP NEED TO CONVERT TO SEPERATE QCLASS
        QString timestamp;             // 0
        QString unit_id;               // 1
        QString accel_x;               // 2
        QString accel_y;               // 3
        QString accel_z;               // 4
        QString accel_temp;            // 5
        QString unit_pressure;         // 6
        QString unit_temp;             // 7
        QString bpm_instant;           // 8
        QString bpm_average;           // 9
        QString unit_rssifreq;         // 10
        QString unit_rssiduty;                          // 11
        QString adc_1; //3.3V regulator                 // 12
        QString adc_2; //RSSI incoming signal strength  // 13
        QString adc_3 ; //gas sensor (propane)           // 14
        QString adc_4 ; //5V regulator                   // 15
        QString unit_checksum;                          // 16
        QString base_checksum;                          // 17
        QString base_pressure;                          // 18
        QString base_temp;                              // 19
        QString rssi_1;                                 // 20
        QString rssi_2;                                 // 21
        QString rssi_3;                                 // 22
        QString rssi_4;                                 // 23
        QString rssi_avgcount;                          // 24
    };

    explicit UnitWidget(QWidget *parent = 0);
    ~UnitWidget();
    void initDatabase();
    void updateInfo(const UnitData &data);
    void storeMessage(QByteArray message);
    bool isDBset();
    void insertDatabase(UnitData insertdata);
    void initTcp();
    void sendTcp(UnitData senddata);

private:
    QTcpServer *tcpServer;
    QNetworkSession *networkSession;
    bool boolTcp;

    Ui::UnitWidget *ui;

    UnitData storedmessage;
    QByteArray fullmsg;
    QByteArray tempmsg;

    bool databaseset;
    QSqlDatabase db;
    QString tablename;
    qint64 tablecounter;



};

#endif // UNITWINDOW_H
